//
//  HMVisitorViewController.swift
//  WeiBo
//
//  Created by Apple on 2017/5/30.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

//  访客视图控制器
class HMVisitorViewController: UITableViewController {

    //  登录标记
    var isLogin: Bool = HMUserAccountViewModel.sharedUserAccount.isLogin
    //  访客视图
    var visitorView: HMVisitorView?
    
    override func loadView() {
        if isLogin {
            super.loadView()
            
        } else {
            //  自定义视图
            visitorView = HMVisitorView()
            //  设置代理
            visitorView?.delegate = self
            //  设置闭包
            visitorView?.loginCallBack = { [unowned self] in
                print("闭包回调登录")
                self.enterOAuthVC()
            }
            view = visitorView
            setupNavUI()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    //  设置导航栏按钮
    fileprivate func setupNavUI() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(registerAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: #selector(loginAction))
        navigationItem.title = "微博登录"
    }
    
    //  MARK:   --点击导航栏按钮事件
    @objc private func registerAction() {
        print("注册")
        enterOAuthVC()
    }
    
    
    @objc private func loginAction() {
        print("登录")
        enterOAuthVC()
    }

    //  进入第三方登录界面
    fileprivate func enterOAuthVC() {
        let oauthVC = HMOAuthViewController()
        let nav = HMNavigationController(rootViewController: oauthVC)
        self.present(nav, animated: true, completion: nil)
    }
    
}

extension HMVisitorViewController: HMVisitorViewDelegate {
    func loginOperation() {
        print("代理回调")
    }
}
